<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/jsp/comm_css_include.jsp" %>
<!-- jqgrid-->
<link href="${basePath}/css/plugins/jqgriddemo/css/ui.jqgrid-bootstrap.min.css" rel="stylesheet" />
<!doctype html>
<html>
<head>
</head>
<body class="gray-bg">
<div class="creditbody">
    <div class="iboxtitle">
        <span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;续保派单</span>
        <a href="javascript:void(0)"></a>
    </div>
    
    <div class="ibox-content ibox-con">
    	<button type="button" class="btn dpbtn_blue btnaplly" onclick="allocation();">确定派单</button>
        <form class="form-horizontal" id="commentForm">
            <table cellpadding="0" cellspacing="0" border="0" class="buyinfor-table zx-addtable">
            	<tr>
            		<td align="right">
						<label class="control-label np">当前部门：</label>
					</td>
            	
					<td>
						<input type="text" class="form-control" readonly="readonly" value="${current_user_key.orgName}">
					</td>
					<td align="right">
						<label class="control-label np">部门主管：</label>
					</td>
            	
					<td>
                       <input type="text" class="form-control" readonly="readonly" value="${current_user_key.uname}">
					</td>
					<td align="right">
						<label class="control-label np">当前未分配客户数：</label>
					</td>
            	
					<td>
                        <input class="form-control" readonly="readonly" value="${countNotAllocation }" >
					</td>
				</tr>
            </table>
            <div class="average-man">平均每个业务员可领到${avg }单</div>
			
			<div class="send-tab" id="send-tab">
           		<ul class="send-orderul">
	           		<li class="send-active" id="randomAllocation"><i class="fa fa-dot-circle-o"></i>随机派单</li>
	           		<li id="randomForce"><i class="fa fa-circle-o"></i>强制指定派单</li>
	           	</ul>
           		<div id="send-content">
	           		<div class="send-div" style="display: block;">
	           			<ul class="send-numa">
	           				<li>单次分派单数：</li>
	           				<li><input type="number" min="1"  max="${countNotAllocation }" id="allocationNum" name="allocationNum" value="0" class="form-control" required="" onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');this.value=this.value.replace('.','');"/></li>
	           				<li>（1~100的整数）</li>
	           			</ul>	
	           		</div>
	           		
	           		<div class="send-div"> 
	           			<ul class="send-numa">
	           				<li>设定操作员：</li>
	           				<li>
	           					<mytag:selectTag others="class='select-control' required='required'" id="operatorId" name="operatorId" map="${users }"  />
	           				</li>
	           			</ul>
	           			
	           			<div class="sendcell-wid">	
				          	<div class="jqGrid_wrapper">
					             <table id="table_list_1"></table>
					             <div id="pager_list_1"></div>
				         	</div>
				        </div>
	           		</div>
	           	</div>	
           	</div>
     	</form>  
     </div>     
    
</div>

<!-- public quote -->
<%@include file="/jsp/comm_js_include.jsp" %>

<!-- jqGrid -->
    <script type="text/javascript" charset="utf-8" src="${basePath}/js/plugins/jqgriddemo/js/jquery.jqGrid.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="${basePath}/js/plugins/jqgriddemo/js/grid.locale-cn.min.js"></script>	

<script type="text/javascript">
	jQuery(function(){
    	getData();
    	//选项卡
        var $ul = $('#send-tab>ul li');
		var $div = $('#send-content .send-div');
        $ul.click(function(){
			var $this = $(this);
			var $t = $this.index();
			$thisIndex = $t;
			$ul.removeClass();
			$this.addClass('send-active');
			$this.find("i").addClass('glyphicon glyphicon-record');
			$this.siblings().find("i").removeClass('glyphicon glyphicon-record');
			$div.css('display','none');
			$("#table_list_1").setGridWidth($(window).width()-100);
			$div.eq($t).css('display','block');
		});	 
	})
		
		var winWidth = $(window).width();
		var winHeight = $(window).height();
	    var getData = function()
	    {
   	   	   winWidth = $(window).width();
   	   	   winHeight = $(window).height() / 2.3;
		   var win = winWidth / 7 - 40;
  		
  	 	   $.jgrid.defaults.styleUI = 'Bootstrap';
  	 	   
		  jQuery("#table_list_1").jqGrid
		  ({
			url:'${basePath}/DhInsuranceRenewalController/notAllocationList',
	        datatype : "json",
	        colNames : [ '主键','保险到期日', '剩余天数', '保险公司','年度','车辆', '车价', '业务员', '客户姓名','身份证号码'],
	        colModel : [ 
	                     {name: 'replaceId', index: 'id', hidden:true},
	            		 {name: 'dateDue',index: 'dateDue',width: win,align: "center",formatter:function(a,b,c){
	                    	 return  a ? formateStringDate(a) : '-';
	                     }},
	            		 {name: 'dateDue',index: 'dateDue',width: win,align: "center",formatter:function(a,b,c){
	                    	 return  a?dateDiff(a):"-";
	                     }},
	            		 {name: 'nameCompany',index: 'nameCompany',width: win,align: "center"},
	                     {name: 'year',index: 'year',width: win,align: "center"},
	                  	 {name: 'nameCar',index: 'nameCar',width: win,align: "center"},
	                  	 {name: 'price',index: 'price',width: win,align: "center"},
	               		 {name: 'nameSalesman',index: 'nameSalesman',width: win,align: "center"},
	                     {name: 'nameCus',index: 'nameCus',width: win,align: "center"},
	            		 {name: 'idnoCus',index: 'idnoCus',width: win,align: "center"}
	                   ],
              mtype : "post",
              height: winHeight,
              width:winWidth,  
              autowidth: false,
              shrinkToFit: false,
              rowNum: 15,
              rowList: [15, 30, 45],
              viewrecords: true,
              pager: "#pager_list_1",
              hidegrid: false,
              multiselect : true,
              altRows:true,
              altclass:'someClass',
              jsonReader : {
            	  root:"result.rows",
            	  page: "result.page",
            	  total: "result.total",
            	  records: "result.records",
            	  repeatitems: false,
            	  id: "replaceId"
              },
              loadComplete:function(datas){
	         	  if (datas.flag == "100100") {
	                   layer.msg("查询成功!", {icon: 6, time: 1000});
	                   return;
	               }
	         	  if(datas.flag == '100802') {
	 				  top.location.href = thisLoginUrl;
	 				  return;
	 			  }

	         	  layer.msg(datas.message, {icon: 2});
	           }
	      });
	    }	    
	    
	    window.onresize = function()
	    {
	    	$("#table_list_1").setGridWidth($(window).width()-80);
	    }
	    
	    
	    var refresh = function()
	    {
	    	$("#table_list_1").jqGrid("setGridParam", { search: true }).trigger("reloadGrid", [{ page: 1}]);
	    }

	    function allocation(){
	    	var data = {};
	    	var url = '';
	    	if($('#randomAllocation').hasClass('send-active')){
	    		if($("#allocationNum").val()==""){
					layer.msg("分派数不能为空", {icon: 5});
					return;
				}
	    		if($("#allocationNum").val() <= 0){
					layer.msg("分派数不能小于0", {icon: 5});
					return;
				}
	    		
	    		if($("#allocationNum").val() > 100){
					layer.msg("分派数不能大于100", {icon: 5});
					return;
				}
	    		data.allocationNum  = $('#allocationNum').val();
	    		url = '${basePath}/DhInsuranceRenewalController/randomAllocation';
	    	} else {
	    		url = '${basePath}/DhInsuranceRenewalController/allocation';
		    	var ids = $('#table_list_1').jqGrid('getGridParam','selarrrow').join(",");
		    	if(!ids || ids == ""){
		    		layer.msg("请选择要分配的续保单!", {icon: 5});
		    		return;
		    	}
		    	data.id = ids;
		    	data.operatorId = $('#operatorId').val();
		    	data.operatorName = $('#operatorId').children('option:selected').text();
	    	}
	    	 $.dpAjax({
			 		url:url,
					data:data,
					success: function(datas){
		 			  if(datas.flag == "100100") {
		 				 layer.msg("分配成功!", {icon: 6});
		 				 location.reload();
					     return;
					   }
					   if(datas.flag == "100200") {
						  $.fn.showDatas({"datas":datas});
		             	  return;
		               }
					layer.msg(datas.message, {icon: 2});
		         }
		 	 });
	    }
	    
	    function formateStringDate(sDate1){
	    	if(!sDate1 || sDate1 == ''){
	    		return '-';
	    	}
	    	var aDate  =  sDate1.split(" ");
	    	return aDate[0];
	    }
	    
	   //计算天数差的函数，通用  
	    function dateDiff(sDate1){
	        var  aDate,  oDate1,  iDays  
	        aDate  =  sDate1.split("-")  
	        oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2].split(" ")[0]  +  '-'  +  aDate[0]) //转换为12-18-2006格式  
	        
	        sDate2  =  new  Date(); 
	        var year2 = sDate2.getFullYear();
	        var month2 = sDate2.getMonth() + 1;
	        var day2 = sDate2.getDate();
	        var oDate2 = new  Date(month2 + '-' + day2 + '-' + year2 );
	        
	        var diffDate = (oDate1  -  oDate2)  /  1000  /  60  /  60  /24;//把相差的毫秒数转换为天数  
	        var iDays  =  parseInt(diffDate);
	        if(iDays > 0){
	        	return iDays;
	        } else if(iDays < 0){
	        	return '<span style="color:#FC1515;">已超期'+  Math.abs(iDays) + '天</span>';
	        } else {
	        	return '<span style="color:#FC1515;">今日到期</span>';
	        }
	    } 
</script>
   
</body>
</html>